package com.hghivln.controller.erp;

import com.hghivln.aspectj.annotation.Log;
import com.hghivln.aspectj.enums.BusinessType;
import com.hghivln.controller.BaseController;
import com.hghivln.pojo.dto.MedicinesDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.MedicinesService;
import com.hghivln.utils.ShiroUtils;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;

@RestController
@RequestMapping("erp/medicines")
public class MedicinesController extends BaseController {

    @Reference
    private MedicinesService medicinesService;


    /**
     * 分页查询
     */
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "10000")
    })
    @GetMapping("listMedicinesForPage")
    public AjaxResult listMedicinesForPage(MedicinesDto medicinesDto) {
        return medicinesService.listMedicinesForPage(medicinesDto);
    }


    /**
     * 添加
     */
    @HystrixCommand
    @PostMapping("addMedicines")
    @Log(title = "添加药品信息", businessType = BusinessType.INSERT)
    public AjaxResult addMedicines(@Validated MedicinesDto medicinesDto) {
        medicinesDto.setSimpleUser(ShiroUtils.getCurrentSimpleUser());
        return medicinesService.addMedicines(medicinesDto);
    }

    /**
     * 修改
     */
    @HystrixCommand
    @PutMapping("updateMedicines")
    @Log(title = "修改药品信息", businessType = BusinessType.UPDATE)
    public AjaxResult updateMedicines(@Validated MedicinesDto medicinesDto) {
        medicinesDto.setSimpleUser(ShiroUtils.getCurrentSimpleUser());
        return medicinesService.updateMedicines(medicinesDto);
    }

    /**
     * 删除
     */
    @HystrixCommand
    @DeleteMapping("deleteMedicinesByIds/{ids}")
    @Log(title = "删除药品信息", businessType = BusinessType.DELETE)
    public AjaxResult deleteMedicinesByIds(@PathVariable @Validated @NotEmpty(message = "要删除的药品信息ID不能为空") Long[] ids) {
        return medicinesService.deleteMedicinesByIds(ids);
    }

    /**
     * 根据ID查询一个药品信息
     */
    @HystrixCommand
    @GetMapping("getMedicinesById/{id}")
    public AjaxResult getMedicinesById(@PathVariable @Validated @NotEmpty(message = "药品信息ID不能为空") Long id) {
        return medicinesService.selectMedicinesById(id);
    }

    /**
     * 查询所有有效的药品信息
     */
    @HystrixCommand
    @GetMapping("selectAllMedicines")
    public AjaxResult selectAllMedicines() {
        return this.medicinesService.selectAllMedicines();
    }

    /**
     * 调整库存
     */
    @HystrixCommand
    @PostMapping("updateMedicinesStorage/{medicinesId}/{medicinesStockNum}")
    @Log(title = "调整库存", businessType = BusinessType.UPDATE)
    public AjaxResult updateMedicinesStorage(@PathVariable Long medicinesId, @PathVariable Long medicinesStockNum) {
        return medicinesService.updateMedicinesStorage(medicinesId, medicinesStockNum);
    }
}
